home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // GetFontInfo2.c
- // Written by Donald Olson
- // Copyright ®1993 Apple Computer Inc.
- // All rights reserved.
- //
- // This is a simple Scripting Addition that returns the Font (info) of the
- // specified font and size. If the size parameter is not specified, default
- // size of 12 is used. This demonstrates the use of a wildCard event ID
- // as a dispatch mechanism inside your code. Written for a WWDC
- // presentation by Donald Olson and Donn Denman.
- //
- // Syntax: font (leading | ascent | descent | max width) <font name> [size <short>]
- // Returns: record {leading:short;ascent:short;descent:short; max width: short}
- //
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- // Our header
- Boolean GetFontNumber( Str255 fontName, short *fontNum );
-
- // Our Includes
- #include <string.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Errors.h>
- #include <AppleEvents.h>
-
- /*struct FontInfo {
- short ascent;
- short descent;
- short widMax;
- short leading;
- };
- */
- //AEVTFINF****
-
- #define kLeading 'lead'
- #define kAscent 'sent'
- #define kDescent 'desc'
- #define kMaxWidth 'mxwd'
-
- #define keyFONTSIZE 'FSIZ'
-
- pascal OSErr main(AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSErr theErr = noErr;
- GrafPort fontPort;
- GrafPtr savedPort, fontPortPtr = &fontPort;
- Str255 fontName;
- long actualSize;
- short fontSize, fontNum;
- DescType typeCode;
- FontInfo fInfo;
- DescType ourID;
-
- theErr = AEGetAttributePtr(theEvent, keyEventIDAttr,
- typeKeyword, &typeCode, (Ptr)&ourID,
- sizeof(ourID), &actualSize);
-
- theErr = AEGetParamPtr(theEvent, keyDirectObject,
- typeChar, &typeCode, (Ptr)&fontName,
- sizeof(fontName), &actualSize);
-
- if(theErr != noErr)
- return theErr;
-
- fontName[actualSize] = '\0'; // c string please
- c2pstr((char*)fontName); // and now pascal
-
- theErr = AEGetParamPtr(theEvent, keyFONTSIZE,
- typeShortInteger, &typeCode, (Ptr)&fontSize,
- sizeof(fontSize), &actualSize);
-
- if(theErr != noErr)
- fontSize = 12;
-
- if(!GetFontNumber(fontName, &fontNum))
- return fontDecError;
-
- GetPort(&savedPort);
- OpenPort(&fontPortPtr);
- SetPort(&fontPortPtr);
-
- TextFont(fontNum);
- TextSize(fontSize);
-
- GetFontInfo(&fInfo);
-
- SetPort(&savedPort);
-
- switch(ourID) {
- case kLeading:
- theErr = AEPutParamPtr( theReply, keyDirectObject,
- typeShortInteger,
- (Ptr)&fInfo.leading,
- sizeof(fInfo.leading));
- break;
- case kAscent:
- theErr = AEPutParamPtr( theReply, keyDirectObject,
- typeShortInteger,
- (Ptr)&fInfo.ascent,
- sizeof(fInfo.ascent));
- break;
- case kDescent:
- theErr = AEPutParamPtr( theReply, keyDirectObject,
- typeShortInteger,
- (Ptr)&fInfo.descent,
- sizeof(fInfo.descent));
- break;
- case kMaxWidth:
- theErr = AEPutParamPtr( theReply, keyDirectObject,
- typeShortInteger,
- (Ptr)&fInfo.widMax,
- sizeof(fInfo.widMax));
- break;
- default:
- break;
- }
-
- return theErr;
- }
-
- Boolean GetFontNumber( Str255 fontName, short *fontNum )
- {
- Str255 systemFontName;
-
- GetFNum( fontName, fontNum );
- if ( *fontNum == 0) {
- /* Either we didn't find the font, or we were looking for the system
- * font. */
- GetFontName( 0, systemFontName );
- return EqualString( fontName, systemFontName, FALSE, FALSE );
- }
- else
- return TRUE;
- }
-